package com.jpz.controller;


import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.jpz.bean.Depart;
import com.jpz.bean.Emp;
import com.jpz.bean.User;
import com.jpz.service.DepartService;
import com.jpz.service.EmpService;
import com.jpz.utils.pageshow;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jpz
 * @since 2018-11-12
 */
@Controller
@RequestMapping("/emp")
public class EmpController {
	@Autowired
	DepartService departservice;
	@Autowired
	EmpService empservice;
	@RequestMapping("/show")
	public String show(Integer pageindex,Model model) {
		Page<Emp> page = new Page<>(pageindex, 4);
		Page<Emp> page2 = empservice.selectPage(page, new EntityWrapper<Emp>().eq("del", 0));
		List<Emp> list = page2.getRecords();
		for (Emp emp : list) {
			//查询部门的名字
			Depart depart = departservice.selectById(emp.getDid());
			emp.setDepartname(depart.getName());
		}
		//共多少项
		long total = page2.getTotal();
		int i = new Long(total).intValue();
		pageshow<Emp> pageshow = new pageshow<>(list, i, null, page2.getCurrent());
		model.addAttribute("pagebean", pageshow);
		return "/emplist.jsp";
	}
	@RequestMapping("/update")
	public String update(Integer eid,Model model) {
		//获取对象用来回显
		Emp emp = empservice.selectById(eid);
		//获取所有的部门用来回想
		List<Depart> dpartl = departservice.selectList(null);
		model.addAttribute("emp", emp);
		model.addAttribute("dpartl", dpartl);
		return "/empupdate.jsp";
	}
	@RequestMapping("/reallup")
	public String reallup(@RequestParam("file") MultipartFile file,HttpServletRequest request,Emp emp,Model model,int eid) {
		
			empservice.updateById(emp);
			if(file.getSize()!=0) {
				//上传的代码
				File path = createDir(request.getSession().getServletContext());
				String fileName=createName(file.getOriginalFilename());
				File f=new File(path, fileName);
				if (!file.isEmpty()) {
					try {
						file.transferTo(f);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				//对路径的操作
				String absolutePath = f.getAbsolutePath();
				int indexOf = absolutePath.indexOf("Dida");
				String substring = absolutePath.substring(indexOf+5);
				String replaceAll = substring.replaceAll("\\\\", "/");
				//将路径存入数据库
				Emp emp2 = empservice.selectById(eid);
				emp2.setPhoto(replaceAll);
				empservice.updateById(emp2);
				model.addAttribute("user", emp2);
			}
			//存值到页面改变后  用于显示
			return "redirect:/emp/show?pageindex=1";
		}
	@RequestMapping("/del")
	public String del(Model model,int eid) {
		
		Emp emp = empservice.selectById(eid);
		emp.setDel(1);
		empservice.updateById(emp);
		return "redirect:/emp/show?pageindex=1";
	}
	@RequestMapping("/add")
	public String add(Model model) {
		List<Depart> list = departservice.selectList(null);
		model.addAttribute("depart", list);
		return "/empadd.jsp";
	}
	@RequestMapping("/realladd")
	public String realladd(@RequestParam("file") MultipartFile file,HttpServletRequest request,Emp emp,Model model) {
		//先将emp对象存入数据库 有了ID之后再存照片
		boolean insert = empservice.insert(emp);
		//获取eid
		Integer eid = emp.getId();
		//上传的代码
		File path = createDir(request.getSession().getServletContext());
		String fileName=createName(file.getOriginalFilename());
		File f=new File(path, fileName);
		if (!file.isEmpty()) {
			try {
				file.transferTo(f);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		//对路径的操作
		String absolutePath = f.getAbsolutePath();
		int indexOf = absolutePath.indexOf("Dida");
		String substring = absolutePath.substring(indexOf+5);
		String replaceAll = substring.replaceAll("\\\\", "/");
		//将路径存入数据库
		Emp emp2 = empservice.selectById(eid);
		emp2.setPhoto(replaceAll);
		empservice.updateById(emp2);
		return "redirect:/emp/show?pageindex=1";
	}
	
	
	// 创建目录---以日期，一天一个文件夹
		private File createDir(ServletContext context) {
			String realPath = context.getRealPath("/emppic");
			String date = new SimpleDateFormat("yyyy-MM-dd").format(Calendar.getInstance().getTime());
			File file = new File(realPath, date);
			if (!file.exists()) {
				file.mkdir();
			}
			return file;
		}
		// 创建文件名--区分同名文件,在文件名前加上当前的时间
		private String createName(String name) {
			return new SimpleDateFormat("yyyyMMddHHmmssSSS").format(Calendar.getInstance().getTime()) + "_" + name;
		}
	
	
	}



